package org.cs2340.jellybeanwms.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;

/**
 * This is the actual implementation of the Model interface.
 * It uses the Monostate pattern, so all instances share one 
 * set of data.
 * 
 * @author Robert
 *
 */
public class InMemoryModel implements IModel {
	/** the data store of all people */
	private static Map<String, Person> people = new HashMap<String, Person>();
	
	//Initialize some people for the model and put them in the collection
	static {
		people.put("rw888", new Person("bob" , "rw888", "bob@foo.com"));
		people.put("aaa8", new Person("alice", "aaa8", "alice@foo.com"));
		people.put("t54",new Person("terry", "t54", "terry@bar.com"));
	}

	@Override
	public void addPerson(Person p) {
		people.put(p.getUid(), p);	
		Log.d("Model", "Person added: " + p.getUid());
	}

	@Override
	public Person findPersonById(String uid) {
		return people.get(uid);
	}

	@Override
	public void removePerson(String uid) {
		people.remove(uid);		
	}

	@Override
	public Person[] getPeople() {
		//Making a copy of data to pass over trust boundary
		Collection<Person> p = people.values();
		Person[] retVal = new Person[p.size()];
		int i = 0;
		for (Person p1 : p) retVal[i++] = p1;
		return retVal;
	}

}
